package com.keter.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;

import java.util.Map;

/**
 * 为转换后的token补充id字段
 * Created by gulx@neusoft.com on 2020/6/8.
 */
public class MyAccessTokenConverter implements AccessTokenConverter {
    private static final Logger logger = LoggerFactory.getLogger(MyAccessTokenConverter.class);

    DefaultAccessTokenConverter defaultAccessTokenConverter = new DefaultAccessTokenConverter();

    @Override
    public Map<String, ?> convertAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
        Map<String, Object> map = (Map<String, Object>) defaultAccessTokenConverter.convertAccessToken(token,authentication);
        map.put("id", ClientUserUtil.instance().retrieveClientDetails(authentication.getName()).getId());
        logger.info("result:{}",map);
        return map;
    }

    @Override
    public OAuth2AccessToken extractAccessToken(String value, Map<String, ?> map) {
        return defaultAccessTokenConverter.extractAccessToken(value,map);
    }

    @Override
    public OAuth2Authentication extractAuthentication(Map<String, ?> map) {
        return defaultAccessTokenConverter.extractAuthentication(map);
    }

}
